package com.nit.lab.dao.Impl;

import com.nit.lab.dao.FileDao;
import com.nit.lab.dto.DtoStore;
import com.nit.lab.dto.DtoSysFile;
import com.nit.lab.model.SysFileEntity;
import com.nit.lab.utils.SystemConstants;
import org.hibernate.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author be_yond
 * @date 2017/10/24
 */
@Repository
public class FileDaoImpl implements FileDao {

    @Resource
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Override
    public Integer getFile4DataTableCount(String search, Integer pre, Integer actorId) throws Exception {
        Integer result = 0;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        if (actorId == 1) {
            hql = "select count(*) from SysFileEntity where isDelete = 0" +
                    "and concat(fileName, keyWord) like '%" + search + "%'";
            query = session.createQuery(hql);
        } else {
            hql = "select count(*) from SysFileEntity sf,SysUserEntity se where sf.userId = se.userId and sf.isDelete = 0 and se.userId =:pre " +
                    "and concat(fileName, keyWord) like '%" + search + "%'";
            query = session.createQuery(hql);
            query.setInteger("pre", pre);
        }
        result = Integer.parseInt(query.uniqueResult().toString());
        return result;
    }

    @Override
    public List<DtoSysFile> getFileDataTable(Integer start, Integer length, String search, Integer pre, Integer actorId) throws Exception {
        List<DtoSysFile> result = null;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        if (actorId == 1) {
            hql = "select new com.nit.lab.dto.DtoSysFile(se.userName,se.userRealName ,sf.fileId, sf.fileName, sf.fileBelong, sf.fileUrl, sf.keyWord, sf.fileFormat, sf.isPublic," +
                    " sf.userId, sf.price, sf.browseCount, sf.downloadCount, sf.uploadTime) " +
                    "from SysFileEntity sf,SysUserEntity se where sf.userId = se.userId and sf.isDelete = 0 " +
                    "and concat(fileName, keyWord) like '%" + search + "%'";
            query = session.createQuery(hql);
        } else {
            hql = "select new com.nit.lab.dto.DtoSysFile(se.userName,se.userRealName ,sf.fileId, sf.fileName, sf.fileBelong, sf.fileUrl, sf.keyWord, sf.fileFormat, sf.isPublic," +
                    " sf.userId, sf.price, sf.browseCount, sf.downloadCount, sf.uploadTime) " +
                    "from SysFileEntity sf,SysUserEntity se where sf.userId = se.userId and sf.isDelete = 0 and se.userId =:pre" +
                    " and concat(fileName, keyWord) like '%" + search + "%'";
            query = session.createQuery(hql);
            query.setInteger("pre", pre);
        }
        result = query.setFirstResult(start).setMaxResults(length).list();
        return result;
    }

    @Override
    public Integer uploadFile(SysFileEntity file) throws Exception {
        Session session = sessionFactory.getCurrentSession();
        return (Integer) session.save(file);
    }

    @Override
    public Integer updataFileInfo(SysFileEntity sysFileEntity) throws Exception {
        String hql = "update SysFileEntity set fileName =:fileName , keyWord =:keyWord ,isPublic =:isPublic,price =:price where fileId =:fileId";
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setString("fileName", sysFileEntity.getFileName());
        query.setString("keyWord", sysFileEntity.getKeyWord());
        query.setInteger("isPublic", sysFileEntity.getIsPublic());
        query.setDouble("price", sysFileEntity.getPrice());
        query.setInteger("fileId", sysFileEntity.getFileId());
        return query.executeUpdate();
    }

    @Override
    public String deleteFile(Integer fileId) throws Exception {
        String hql = "update SysFileEntity set isDelete = 1 where fileId =:fileId ";
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setInteger("fileId", fileId);
        return query.executeUpdate() >= 0 ? "Success" : "Failure";
    }

    @Override
    public List<DtoSysFile> getFileDataTableForUser(Integer start, Integer length, String search, Integer sessionId) throws Exception {
        List<DtoSysFile> result = null;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        hql = "select new com.nit.lab.dto.DtoSysFile(se.userName ,sf.fileId, sf.fileName, sf.fileBelong, sf.fileUrl, sf.keyWord, sf.fileFormat, sf.isPublic," +
                " sf.userId, sf.price, sf.browseCount, sf.downloadCount, sf.uploadTime, st.storeName,st.storeId) " +
                "from SysFileEntity sf,SysUserEntity se ,SysStoreEntity st where sf.userId = se.userId and se.userId = st.userId and sf.isDelete = 0 and sf.isPublic = 0 and se.userId !=:sessionId " +
                "and concat(fileName, keyWord) like '%" + search + "%'";
        query = session.createQuery(hql);
        query.setInteger("sessionId", sessionId);
        result = query.setFirstResult(start).setMaxResults(length).list();
        return result;
    }

    @Override
    public Integer getFile4DataTableForUserCount(String search, Integer sessionId) throws Exception {
        Integer result = 0;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        hql = "select count(*) from SysFileEntity sf,SysUserEntity se,SysStoreEntity st where sf.userId = se.userId and se.userId = st.userId and sf.isDelete = 0 and sf.isPublic = 0 and se.userId !=:sessionId " +
                "and concat(fileName, keyWord) like '%" + search + "%'";
        query = session.createQuery(hql);
        query.setInteger("sessionId", sessionId);
        result = Integer.parseInt(query.uniqueResult().toString());
        return result;
    }

    @Override
    public List<DtoStore> dtoStores() throws Exception {
        List<DtoStore> result = null;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        hql = "select new com.nit.lab.dto.DtoStore(storeId, userId, storeName, storeAddress, storeNotice, storeEvaluate, isOpen, isDelete) " +
                "from SysStoreEntity ";
        query = session.createQuery(hql);
        result = query.list();
        return result;
    }
}
